var msq = require("./mysql");
var fsql = (url, username, password, library, sql) =>
  new Promise((reslove, reject) => {
    let sqlName = sql.split(' ');
    let newData = null;
    msq.open(url, username, password, library);
    msq.handleData(
      sql,
      null,
      async (result) => {
        msq.close();
        if (sqlName[0] === 'UPDATE') {
          newData = await fsql("8.131.83.251", "root", "123abc", "drugstore", `SELECT * from ${sqlName[1]}`)
        }
        newData && result.affectedRows ? reslove(newData) : reslove(result);
      },
      (err) => {
        msq.close();
        console.log(err);
        reject(err);
      }
    );
  });
module.exports = (sql) =>
  fsql("8.131.83.251", "root", "123abc", "drugstore", sql);
